/*
 * Created on 2004/12/08
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */
package jp.sourceforge.akjrcp.snack;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;


public class PitchParser {
    private String columnSeparator=",";
    public String getColumnSeparator() {
        return columnSeparator;
    }
    public void setColumnSeparator(String columnSeparator) {
        this.columnSeparator = columnSeparator;
    }
    
    public Double[] parse(Reader reader) throws IOException{
        List<Double> result=new ArrayList<Double>();
       
        //result.add(new Double(0));//initial can't parse; but vocaloid2 ignore this
       
        try {
            BufferedReader breader=new BufferedReader(reader);
            String line;
            while((line=breader.readLine())!=null){
                //log.info(line);
                String values[]=null;
                if(line.startsWith("(") && line.endsWith(")")){
                    //log.infoend");
                    values=line.substring(1,line.length()-1).split(",");
                }else{
                    values=line.split(columnSeparator);
                }
                
                    if(values!=null && values.length==4){
                    	result.add(Double.parseDouble(values[0]));
                        }
                    
                
            }
            breader.close();
        } catch (FileNotFoundException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        }
       
        return result.toArray(new Double[result.size()]);
    }
    
  public Double[] parse(InputStream input) throws IOException{
        
        return parse(new InputStreamReader(input));
    }



public Double[] parse(File file) throws IOException{
 
     return parse(new FileReader(file));
 
 }

    /**
     * for 16 note
    public Double[] parse(Reader reader) throws IOException{
        List<Double> result=new ArrayList<Double>();
        double dbs[]={0,0,0,0,0};
        int index=1;//first one is skip
        try {
            BufferedReader breader=new BufferedReader(reader);
            String line;
            while((line=breader.readLine())!=null){
                //log.info(line);
                String values[]=null;
                if(line.startsWith("(") && line.endsWith(")")){
                    //log.infoend");
                    values=line.substring(1,line.length()-1).split(",");
                }else{
                    values=line.split(columnSeparator);
                }
                
                    if(values!=null && values.length==4){
                    	dbs[index]=Double.parseDouble(values[0]);
                        index++;
                        if(index==5){//last;
                        	
                        	result.add(getSixteenthNote(dbs));
                        	
                        	index=0;
                        	dbs=new double[]{0,0,0,0,0};//init
                        }
                    }
                
            }
            breader.close();
        } catch (FileNotFoundException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        }
       
        return result.toArray(new Double[result.size()]);
    }
	private Double getSixteenthNote(double[] dbs) {
		int nozero=0;
		double total=0;
		for(int i=0;i<dbs.length;i++){
			if(dbs[i]!=0){
				total+=dbs[i];
				nozero++;
			}
		}
		if(total<=1){//ignore 0.025 peace
			return new Double(0);
		}
		return new Double(total/nozero);
	}
	
  */

}